package com.changgou.system.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.net.InetSocketAddress;

/**
 * @Description:获取客户端访问的ip
 * @Author: 陈安
 * @Date: 12-05 
 * @Param: 
 * @Return: 
 */
@Component
public class IpFilter implements GlobalFilter,Ordered {

    //定义具体的业务逻辑
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        System.out.println("访问的第一个");
        ServerHttpRequest request = exchange.getRequest();
        InetSocketAddress remoteAddress = request.getRemoteAddress();
        System.out.println("ip:"+remoteAddress.getHostName());
        //放行,让他接着执行接下来的优先级没他高的,要是过滤器都结束了,那他就会进入我们的具体微服务里面了.
        return chain.filter(exchange);
    }

    //定义过滤器的执行优先级的,返回值越小,他的执行优先级就越高,0就是最小的
    @Override
    public int getOrder() {
        return 1;
    }
}
